package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dto.SensitiveDto;
import com.heima.model.admin.pojo.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
//可能需要开启事务
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService{

    @Override
    public ResponseResult list(SensitiveDto dto) {
        //检查参数
        if (null == dto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //封装分页查询条件(分页条件,参数)
        Page<AdSensitive> pageReq = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        //进行分页查询
        IPage<AdSensitive> pageResult = this.page(pageReq, wrapper);
        //响应结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.判断参数
        if (StringUtils.isBlank(adSensitive.getSensitives())){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不能为空");
        }
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在");
        }
        //2.封装
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        //3.响应
        return ResponseResult.okResult();
    }

    /**
     * 修改
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.获取id并检查
        int id = adSensitive.getId();
        if (null==String.valueOf(id)){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不存在");
        }
        AdSensitive adSensitive1 = this.getById(id);  //todo
        if (adSensitive1==null){
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"该词不存在");
        }
        //2.修改
        updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (null ==id){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive = this.getById(id);
        if (adSensitive==null){
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
